﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace TomNet.Attributes
{
    /// <summary>
    /// 邮件地址检查
    /// </summary>
    public class EmailAttribute : BaseAttribute
    {
        public override string Error
        {
            get
            {
                if (base.Error != null)
                {
                    return base.Error;
                }
                return "必须是有效邮箱";
            }

            set => base.Error = value;
        }
        public override bool Validate(object value)
        {
            var regex = new Regex(@"^[!#$%'*+\-\/0-9=?A-Z^_`a-z{}~]+(\.[!#$%'*+\-\/0-9=?A-Z^_`a-z{}~]+)*@(?![\d]+(\.[\d]+)*$)[!#$%'*\/0-9=?A-Z^`a-z{}~]+([!#$%'*\-\/0-9=?A-Z^_`a-z{}~]*[!#$%'*\/0-9=?A-Z^`a-z{}~]+)*(\.[!#$%'*\/0-9=?A-Z^`a-z{}~]+([!#$%'*\-\/0-9=?A-Z^_`a-z{}~]*[!#$%'*\/0-9=?A-Z^`a-z{}~]+)*)+$");
            return value == null || value.ToString().Length == 0 || regex.Match(value.ToString()).Success;
        }
    }
}
